/**
 * 路由拦截与一部分权限控制
 */
import router from '@/router'
import store from '@/store'
import { Message } from 'element-ui'
import { addAsyncRoute } from '@/utils/router'

const asyncRoute = store.getters.ASYNC_ROUTE

if (asyncRoute) {
  addAsyncRoute('', asyncRoute)
}

router.beforeEach(async (to, from, next) => {
  const toPath = to.path
  const env = process.env
  if (env.VUE_APP_MODE === 'prod') {
    window.document.title = `ckmmd-${to.meta.title}`
  } else {
    window.document.title = `ckmmd-${env.VUE_APP_MODE}-${to.meta.title}`
  }
  if (toPath === '/404') {
    next()
  }
  const superUserInfo = store.getters.SUPER_USER_INFO
  if (superUserInfo) {
    if (toPath === '/login') {
      next({ path: '/home' })
      Message({
        message: '已经登陆...',
        type: 'success',
        duration: 1000
      })
    } else {
      next()
    }
  } else {
    next()
  }
})
